package com.cbs.android.extension.recyclerviewadapter.sample;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cbs.android.extension.recyclerviewadapter.RecyclerViewAdapter;
import com.cbs.android.extension.recyclerviewadapter.RecyclerViewSpanSizeLookup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();
    private RecyclerView recyclerView;
    private List<String> dataSource = new ArrayList<>();
    private MyAdapter adapter = new MyAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        /* header
        adapter.setHeaderView(getLayoutInflater().inflate(R.layout.header, recyclerView, false));
        WebView webView = (WebView) adapter.getHeaderView().findViewById(R.id.header);
        webView.loadUrl("http://service.guguxinge.com:8080/pages/news/7145129979715a6ae8d8ebdb3b38d9b8");
        //*/

        //* empty
        adapter.setEmptyView(getLayoutInflater().inflate(R.layout.empty, recyclerView, false));
        TextView textView = (TextView) adapter.getEmptyView().findViewById(R.id.empty);
        textView.setText(System.currentTimeMillis() + "");
        //*/

        recyclerView.setAdapter(adapter);
    }

    public void clear(View view) {
        dataSource.clear();
        adapter.notifyDataSetChanged();
    }

    public void fill(View view) {
        for (int i = 0; i < 20; i++) {
            dataSource.add("item" + i);
        }
        adapter.notifyDataSetChanged();
    }

    public void linear(View view) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    public void grid(View view) {
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        layoutManager.setSpanSizeLookup(new RecyclerViewSpanSizeLookup(adapter, layoutManager));
        recyclerView.setLayoutManager(layoutManager);
    }

    private class MyAdapter extends RecyclerViewAdapter {
        @Override
        protected RecyclerView.ViewHolder onCreateContentViewHolder(ViewGroup parent, int viewType) {
            View view = getLayoutInflater().inflate(R.layout.content, parent, false);
            return new ContentViewHolder(view, viewType);
        }

        @Override
        protected void onBindContentViewHolder(RecyclerView.ViewHolder holder, int position) {
            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
            contentViewHolder.contentView.setText(dataSource.get(position));
        }

        @Override
        public int getContentCount() {
            return dataSource.size();
        }

        @Override
        protected int getContentViewType(int position) {
            return position % 2;
        }

        private class ContentViewHolder extends RecyclerView.ViewHolder {
            public TextView contentView;

            public ContentViewHolder(View itemView, int type) {
                super(itemView);
                contentView = (TextView) itemView.findViewById(R.id.content);
                if (type == 0) {
                    contentView.setBackgroundColor(Color.RED);
                } else {
                    contentView.setBackgroundColor(Color.GREEN);
                }
            }
        }
    }
}
